package com.bdqn.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.bus.entity.Car;
import com.bdqn.bus.entity.Check;
import com.bdqn.bus.entity.Customer;
import com.bdqn.bus.entity.Rent;
import com.bdqn.bus.service.CarService;
import com.bdqn.bus.service.CheckService;
import com.bdqn.bus.service.CustomerService;
import com.bdqn.bus.service.RentService;
import com.bdqn.bus.vo.CheckVo;
import com.bdqn.common.utitls.DateGindViewResult;
import com.bdqn.common.utitls.JSONResult;
import com.bdqn.common.utitls.SystemConstants;
import com.bdqn.common.utitls.UUIDUtil;
import com.bdqn.sys.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author KazuGin
 * @since 2020-05-15
 */
@RestController
@RequestMapping("/bus/check")
public class CheckController {



    @Resource
    private CheckService checkService;

    @Resource
    private RentService rentService;

    @Resource
    private CustomerService customerService;

    @Resource
    private CarService carService;

    @RequestMapping("/initCheckFormData")
    public Map<String,Object> initCheckFormData(String rentid, HttpSession session){
        //出租单信息
        QueryWrapper<Rent> rentQueryWrapper = new QueryWrapper<Rent>();
        rentQueryWrapper.eq("rentid",rentid);//出租单号
        //查询出租单信息
        Rent rent = rentService.getOne(rentQueryWrapper);

        //客户信息
        QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<Customer>();
        customerQueryWrapper.eq("identity",rent.getIdentity());//身份证号码
        Customer customer = customerService.getOne(customerQueryWrapper);

        //汽车信息
        //根据车牌号码查询车辆信息
        QueryWrapper<Car> carQueryWrapper = new QueryWrapper<Car>();
        carQueryWrapper.eq("carnumber",rent.getCarnumber());//车牌号码
        Car car = carService.getOne(carQueryWrapper);

        //获取当前登录用户
        User user = (User) session.getAttribute(SystemConstants.LOGIN_USER);

        //封装检查单数据
        Check check = new Check();
        check.setCheckid(UUIDUtil.createRandomStringUseTime("JC"));//检查单单号
        check.setRentid(rentid);//对应检查的出租单号
        check.setCheckdate(new Date());//检查时间
        check.setOpername(user.getName());//检查人

        //将数据放到map集合中
        Map<String,Object> map = new LinkedHashMap<String,Object>();
        map.put("rent",rent);//出租单信息
        map.put("customer",customer);//客户信息
        map.put("car",car);//车辆信息
        map.put("check",check);//检查单

        return map;
    }


    @RequestMapping("/addCheck")
    public JSONResult  addCheck(Check check,Integer carId){
        //调用添加检查单的方法
        if (checkService.addCheck(check,carId)){
            return   SystemConstants.Add_SUCCESS;
        }
        return   SystemConstants.Add_ERROR;

    }


    @RequestMapping("/list")
    public DateGindViewResult list(CheckVo checkVo){
        IPage<Check> page=new Page<Check>(checkVo.getPage(),checkVo.getLimit());

        QueryWrapper<Check> queryWrapper=new QueryWrapper<Check>();
        queryWrapper.like(StringUtils.isNotBlank(checkVo.getCheckid()),"checkid",checkVo.getCheckid());//检查单号
        queryWrapper.like(StringUtils.isNotBlank(checkVo.getRentid()),"rentid",checkVo.getRentid());//出租单号
        queryWrapper.like(StringUtils.isNotBlank(checkVo.getProblem()),"problem",checkVo.getProblem());//问题
        queryWrapper.like(checkVo.getCheckdesc()!=null,"checkdesc",checkVo.getCheckdesc());//描述
        queryWrapper.ge(checkVo.getStartTime()!=null,"checkdate",checkVo.getStartTime());//开始时间
        queryWrapper.le(checkVo.getEndTime()!=null,"checkdate",checkVo.getEndTime());//结束时间

        checkService.page(page,queryWrapper);
        return  new DateGindViewResult(page.getTotal(),page.getRecords());
    }


    @RequestMapping("/updataCheck")
    public JSONResult updataCheck(Check check){
        //调用修改检查方法
        if (checkService.updateById(check)){
            return SystemConstants.UPDATE_SUCCESS;
        }
        return  SystemConstants.UPDATE_ERROR;
    }
}

